﻿Imports iConverter.Models
Imports iConverter.Core
Imports System.ComponentModel
Imports iConverter.Configuration
Imports System.Collections.ObjectModel
Imports System.Text
Imports iConverter.Extensions
Imports System.Configuration
Imports iConverter.Contracts


Namespace ViewModels

    ''' <summary>
    ''' PluginSettingsWindows窗口的 ViewModel
    ''' </summary>
    ''' <remarks></remarks>
    Public Class PluginSettingsViewModel
        Implements INotifyPropertyChanged, IDisposable


#Region "INotifyPropertyChanged 的实现 "

        ''' <summary>
        ''' 用来引发 PropertyChanged 事件，当属性被更改之后
        ''' </summary>
        ''' <param name="propName">属性名字</param>
        Private Sub Notify(ByVal propName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
        End Sub

        Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

#End Region



#Region "IDisposable Support"
        Private disposedValue As Boolean ' 检测冗余的调用

        ' IDisposable
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    '  释放托管状态(托管对象)。
                    If Me._win IsNot Nothing Then
                        Me._win.SettingsView.Content = Nothing
                        Me._win.Close()
                    End If


                End If

                '  释放非托管资源(非托管对象)并重写下面的 Finalize()。
                '  将大型字段设置为 null。
                Me._configurator = Nothing
                Me._settings = Nothing
                Me._provider = Nothing
                Me._win = Nothing

            End If
            Me.disposedValue = True
        End Sub

        ' 仅当上面的 Dispose(ByVal disposing As Boolean)具有释放非托管资源的代码时重写 Finalize()。
        'Protected Overrides Sub Finalize()
        '    ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean)中。
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' Visual Basic 添加此代码是为了正确实现可处置模式。
        Public Sub Dispose() Implements IDisposable.Dispose
            ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean)中。
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region



        ''' <summary>
        ''' 显示插件设置对话框
        ''' </summary>
        ''' <param name="provider"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function ShowPluginSettingsDialog(provider As PluginProvider) As Nullable(Of Boolean)
            Dim vm As New PluginSettingsViewModel(provider)

            Return vm.ShowDialog
        End Function




        ''' <summary>
        '''应用此ViewModel的窗体
        ''' </summary>
        ''' <remarks></remarks>
        Dim _win As PluginSettingsWindow


        ''' <summary>
        ''' 创建PluginSettings ViewModel
        ''' </summary>
        ''' <param name="provider">指定插件提供器</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal provider As PluginProvider)
            MyClass.New(New PluginSettingsWindow, provider)

        End Sub
        ''' <summary>
        ''' 创建PluginSettings ViewModel
        ''' </summary>
        ''' <param name="win">指定窗体</param>
        ''' <param name="provider">指定插件提供器</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal win As PluginSettingsWindow, provider As PluginProvider)


            If provider.Settings Is Nothing Then
                Throw New ArgumentException(" 插件不支持配置或禁用")
            End If

            _isAskClosing = True

            Me._provider = provider

            Me._settings = provider.Settings

            AddHandler _settings.PropertyChanged, AddressOf Me.SettingChanged

            Me._win = win
            win.DataContext = Me

            RegisterCommandBindings()

            RegisterWindowsEvent()

            _configurator = provider.Configurator

            _configurator.Load(Me._settings)

            Me._win.SettingsView.Content = _configurator


        End Sub




        ' ''' <summary>
        ' ''' 析构
        ' ''' </summary>
        ' ''' <remarks></remarks>
        'Protected Overrides Sub Finalize()
        '    MyBase.Finalize()
        'End Sub

        ''' <summary>
        ''' 显示对话框
        ''' </summary>
        ''' <remarks></remarks>
        Public Function ShowDialog() As Nullable(Of Boolean)
            Return Me._win.ShowDialog
        End Function

        ''' <summary>
        ''' 当前 插件提供器
        ''' </summary>
        ''' <remarks></remarks>
        Private _provider As PluginProvider

        ''' <summary>
        ''' 当前 插件提供器
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property Provider() As PluginProvider
            Get
                Return _provider
            End Get
        End Property

        ''' <summary>
        ''' 插件配置器
        ''' </summary>
        ''' <remarks></remarks>
        Private _configurator As IPluginConfigurator

        ''' <summary>
        ''' 插件配置器
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property Configurator() As IPluginConfigurator
            Get
                Return _configurator
            End Get
        End Property

        ''' <summary>
        ''' 当前设置
        ''' </summary>
        Private _settings As ApplicationSettingsBase

        ''' <summary>
        ''' 当前设置
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property CurrentSettings() As ApplicationSettingsBase
            Get
                Return _settings
            End Get
        End Property



        ''' <summary>
        ''' 关闭窗体并卸载实例
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub Close(Optional isSkipWindow As Boolean = False)
            Me._isAskClosing = False
            Me.RemoveSettingsChangedHandler()
            UnRegisterWindowsEvent()
            If Not isSkipWindow Then Me._win.Close()
            Me.Dispose()
        End Sub

#Region "检测 设置是否修改"

        ''' <summary>
        ''' 一个值，用来指示在关闭窗体时，是否询问继续关闭
        ''' </summary>
        ''' <remarks></remarks>
        Private _isAskClosing As Boolean

        ''' <summary>
        ''' 一个值，用来指示是否更改过设置
        ''' </summary>
        ''' <remarks></remarks>
        Private _isChanged As Boolean


        ''' <summary>
        ''' 指示是否更改过设置
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property IsChanged() As Boolean
            Get
                Return _isChanged
            End Get
        End Property



        ''' <summary>
        ''' 设置修改事件
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub SettingChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
            If Not Me._isChanged Then
                Me._isChanged = True
            End If
        End Sub


        ''' <summary>
        ''' 移除 Setting 事件注册
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub RemoveSettingsChangedHandler()
            RemoveHandler _settings.PropertyChanged, AddressOf Me.SettingChanged
        End Sub


#End Region





    End Class



End Namespace

